$(function(){
		var tableSource = new $.jqx.dataAdapter({
			dataType: "json",
			dataFields:[
			    { name: "id", type: "int" },
			    { name: "uuid", type: "string" },
			    { name: "user_name", type: "string" },
			    { name: "ruser_name", type: "string" },
			    { name: "puser_name", type: "string" },
			    { name: "tuser_name", type: "string" },
			    { name: "rebate_amt", type: "float" },
			    { name: "rebate_coupon", type: "float" },
			    { name: "rebate_samt", type: "float" },
			    { name: "rebate_scoupon", type: "float" },
			    { name: "rebate_tamt", type: "float" },
			    { name: "rebate_tcoupon", type: "float" },
			    { name: "rebate_product", type: "string" },
			    { name: "rebate_shoptype", type: "string" },
			    { name: "status", type: "string" },
		  	    { name: "createtime", type: "date" },
			],
			id: "id",
			url: "rebate/pagelist.do"
		});
		$("#dataTable").jqxDataTable({
			width: "100%",
	        source: tableSource,
	        pageable: true,
	        serverProcessing: true,
	        pageSize: 18,
	        pagerButtonsCount: 10,
	        columnsResize: true,
	        sortable: true,
	        columns: [
	            { text: "序号", width: '10%', cellsRenderer: function(row) { console.log(tableSource); return (row + 1).toString(); } },
	            { text: "用户", dataField: "user_name", width: '10%' },
	            { text: "一级推荐人", dataField: "ruser_name", width: '10%' },
	            { text: "二级推荐人", dataField: "puser_name", width: '10%' },
	            { text: "三级推荐人", dataField: "tuser_name", width: '10%' },
	            { text: "商品", dataField: "rebate_product", width: 'auto' },
	            { text: "注册类型", dataField: "rebate_shoptype", width: '10%',cellsRenderer: function(row, column, value, rowData){
	        		return ["商户","供应商","代理商"][value];
	        	}},
	            { text: "是否审核", dataField: "status", width: '10%',cellsRenderer: function(row, column, value, rowData){
	        		return ["未审核","已审核"][value];
	        	}},
	            { text: "创建时间", dataField: "createtime", width: 150,
	            	cellsRenderer: function(row, column, value, rowData){
	    				var date = value instanceof Date ? value : new Date(value);
	    				return date.Format("yyyy-MM-dd hh:mm:ss");
	            	}
	            }
	        ],
		});
	$("#dataTable").on("rowDoubleClick",function(info, args){
		if(args && args.flag){
			info = args;
		}
		editingIndex = info.args.index;
		row = info.args.row;
	 	_editerIndex = layer.open({
			id: "sellerEditer",
			title: "查看返利信息",
		    type: 1,
		    skin: 'layui-layer-rim',
		    area: ['1000px', '320px'],
			content : $("#content").html(),
			success : function(e) {
				eConetnt = $(e);
			}
		});
		$("[name=user_name]").val(row.user_name);
		$("[name=ruser_name]").val(row.ruser_name);
		$("[name=puser_name]").val(row.puser_name);
		$("[name=tuser_name]").val(row.tuser_name);
		$("[name=rebate_amt]").val(row.rebate_amt);
		$("[name=rebate_coupon]").val(row.rebate_coupon);
		$("[name=rebate_samt]").val(row.rebate_samt);
		$("[name=rebate_scoupon]").val(row.rebate_scoupon);
		$("[name=rebate_tamt]").val(row.rebate_tamt);
		$("[name=rebate_tcoupon]").val(row.rebate_tcoupon);
		$("[name=rebate_product]").val(row.rebate_product);
		$("[name=rebate_shoptype]").val(row.rebate_shoptype);
		$("[name=status]").val(row.status);
	});
	
	/**
	 * 审核
	 */
	$("#approve").click(function() {
		var rows = isSelected();
		if (rows !== null) {
			approve(rows);
		}
	});
});

/**
 * 审核商品
 */
function approve(rows) {
	var uuids = new Array();
	var isOk = true;
	$.each(rows, function(i) {
		if (rows[i].status === 0) {
			uuids[i] = rows[i].uuid;
		} else {
			isOk = false;
			layer.msg("返利信息已审核");
		}
	});
	if (!isOk) {
		return;
	}
	layer.confirm('审核返利信息', {
		btn : [ '审核通过', '取消' ]
	// 按钮
	}, function() {
		$.post("rebate/approve.do", {
			uuids : uuids
		}, function(result) {
			layer.msg(result.msg);
			if (result.code == 0) {
				var row = rows[0];
				row.status = 1;
				var index = 0;
				var allRows = $("#dataTable").jqxDataTable('getRows');
				for (var i = 0; i < allRows.length; i++) {
					if (allRows[i].uuid === row.uuid) {
						index = i;
					}
				}
				updateRow(index, row);
			}
		});
	}, function() {

	});
}

/**
 * 更新行
 * 
 * @param index
 * @param row
 */
function updateRow(index, row) {
	if (index == null) {
		var rows = $("#dataTable").jqxDataTable('getView');
		for (i = rows.length - 1; i >= 0; i--) {
			if (row.uuid === rows[i].uuid) {
				index = i;
			}
		}
	}
	$("#dataTable").jqxDataTable('updateRow', index, row);
}

/**
 * 是否选中一项
 * 
 * @returns
 */
function isSelected() {
	var rows = $("#dataTable").jqxDataTable("getSelection");
	if (rows.length < 1) {
		layer.msg("请选择一行数据", {
			shift : 6,
			offset : "5%"
		});
		return null;
	}
	return rows;
}